package com.hzmg.route;

import akka.actor.typed.Behavior;
import akka.actor.typed.javadsl.Behaviors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.util.Assert;

import java.net.URI;
import java.util.Objects;
import java.util.function.Predicate;

/**
 * 路由
 * 请求执行流程
 * 请求 --> Predicate --匹配--> Route --过滤--> Filter --> URI
 */
public class Route implements Ordered {
    private static final Logger logger = LoggerFactory.getLogger(Route.class);

/*    public static Behavior<Void> showRouting() {
        return Behaviors.setup(
                context -> {
                    return Behaviors.empty();
                }
        );
    }

    @Override
    public int getOrder() {
        return 0;
    }*/
private final String id;

    // 路由向的 URI
    private final URI uri;

    // 顺序
    private final int order;

    // 谓语数组
    private final Predicate<String> predicate;

    public static Builder builder() {
        return new Builder();
    }

/*    *
            *
    使用 RouteDefinition
    创建对象
     *
    @param
    routeDefinition
     *@return*/

    public static Builder builder(RouteDefinition routeDefinition) {
        return new Builder()
                .id(routeDefinition.getId());
    }

    public Route(String id, URI uri, int order, Predicate<String> predicate) {
        this.id = id;
        this.uri = uri;
        this.order = order;
        this.predicate = predicate;
    }

    public static class Builder {

        private String id;

        private URI uri;

        private int order = 0;

        private Predicate<String> predicate;

        private Builder() {
        }

        public Builder id(String id) {
            this.id = id;
            return this;
        }

        public Builder uri(String uri) {
            this.uri = URI.create(uri);
            return this;
        }

        public Builder uri(URI uri) {
            this.uri = uri;
            return this;
        }

        public Builder order(int order) {
            this.order = order;
            return this;
        }

        public Builder predicate(Predicate<String> predicate) {
            this.predicate = predicate;
            return this;
        }

        //TODO: Filter and webRequest handle

        public Route build() {
            Assert.notNull(this.id, "id can not be null");
            Assert.notNull(this.uri, "uri can not be null");


            return new Route(this.id, this.uri, this.order, this.predicate);
        }
    }


    public String getId() {
        return id;
    }

    public URI getUri() {
        return uri;
    }

    @Override
    public int getOrder() {
        return order;
    }

    public Predicate<String> getPredicate() {
        return predicate;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Route route = (Route) o;
        return Objects.equals(id, route.id) &&
                Objects.equals(uri, route.uri) &&
                Objects.equals(order, route.order) &&
                Objects.equals(predicate, route.predicate);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, uri, predicate);
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Route{");
        sb.append("id='").append(id).append('\'');
        sb.append(", uri=").append(uri);
        sb.append(", order=").append(order);
        sb.append(", predicate=").append(predicate);
        return sb.toString();
    }
}




